/* Includes ------------------------------------------------------------------*/
#include "SystemTick.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TIM4_PERIOD     124
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO bool s_SysIntFlag = FALSE;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/* Public variables ----------------------------------------------------------*/
#define PWM_PRESCALE    100
bool s_PWM_IntFlag = FALSE;
u16  s_PWM_PrescaleCnt = 0;

#define LED7SEG_PRESCALE    5
bool s_Led7Seg_IntFlag = FALSE;
u8   s_Led7Seg_PrescaleCnt = 0;

/* Public functions ----------------------------------------------------------*/


INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
    /* Cleat Interrupt Pending bit */
    TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
    s_SysIntFlag = TRUE;
}


void SysTick_Init(void)
{
    /* TIM4 configuration:
    - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
    clock used is 16 MHz / 128 = 125 000 Hz
    - With 125 000 Hz we can generate time base:
    max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
    min time base is 0.016 ms if TIM4_PERIOD = 1   --> (  1 + 1) / 125000 = 0.016 ms
    - In this example we need to generate a time base equal to 1 ms
    so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */

  /* Time base configuration */
  TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
  /* Clear TIM4 update flag */
  TIM4_ClearFlag(TIM4_FLAG_UPDATE);
  /* Enable update interrupt */
  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  
  /* Enable TIM4 */
  TIM4_Cmd(ENABLE);
}


void SysTick_RunOne(void)
{
    if (TRUE == s_SysIntFlag)
    {
        s_SysIntFlag = FALSE;

        if (PWM_PRESCALE <= ++s_PWM_PrescaleCnt)
        {
            s_PWM_PrescaleCnt = 0;
            s_PWM_IntFlag = TRUE;
        }

        if (LED7SEG_PRESCALE <= ++s_Led7Seg_PrescaleCnt)
        {
            s_Led7Seg_PrescaleCnt = 0;
            s_Led7Seg_IntFlag = TRUE;
        }
    }
}
